package com.qf.controller;

import com.qf.model.Branch;
import com.qf.model.Project;
import com.qf.model.Stage;
import com.qf.page.PageResult;
import com.qf.page.QueryPageBean;
import com.qf.page.Result;
import com.qf.service.BranchService;
import com.qf.service.StageService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/branch")
public class BranchController {

    @Autowired
    private BranchService branchService;

    @RequestMapping("/findAll")
    public List<Branch> findAll(){
        List<Branch> all = branchService.getAll();
        return all;
    }



    /**
     * branch列表分页查询+模糊查询
     * @param queryPageBean
     * @return PageResult
     */
    @RequestMapping("/all")
    public PageResult all(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = branchService.findPage(queryPageBean);
        return pageResult;
    }

    /**
     * 新建branch
     * @param
     * @return Result
     */
    //前端传json
    @RequestMapping("/add")
    public Result add(@RequestBody Branch branch) {
        Result result = new Result();
        int i = branchService.add(branch);
        if (i>0){
            result.setFlag(true);
            result.setMessage("添加成功");
            return result;
        }
        result.setFlag(false);
        result.setMessage("添加失败");
        return result;
    }

    /**
     * 编辑branch回显数据
     * @param bid
     * @return Result
     */
    @GetMapping("/findById")
    public Result findById(@RequestParam("bid") String bid ){
        Result result = new Result();
        Branch branch = branchService.findById(bid);

        if (ObjectUtils.isNotEmpty(branch)){
            result.setData(branch);
            result.setFlag(true);
            result.setMessage("success");
            return result;
        }else {
            result.setFlag(false);
            result.setMessage("fail");
            return result;
        }
    }

    /**
     * 修改branch
     * @param branch
     * @return result
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody Branch branch){
        Result result = new Result();
        int i = branchService.edit(branch);
        if (i>0){
            result.setFlag(true);
            result.setMessage("success");
            return result;
        }
        result.setFlag(false);
        result.setMessage("fail");
        return result;
    }

    /**
     * 删除branch
     * @param bid
     * @return result
     */
    @RequestMapping("/delete")
    public Result delete(@RequestParam("bid") String bid){
        Result result = new Result();
        int i = branchService.delete(bid);
        if (i>0){
            result.setFlag(true);
            result.setMessage("success");
            return result;
        }
        result.setFlag(false);
        result.setMessage("fail");
        return result;
    }
}
